package me.wxide.mysmsfilter;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;


public class MyContentProvider extends ContentProvider implements RuleChains{
	SQLiteDatabase db;
	MyDbConnection dbconn;

	
	  public static final UriMatcher uriMatcher; 
      static { 
                     uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 
                     uriMatcher.addURI(CONTENT_STRING, "rules", MATCH_ITEMS); 
                     uriMatcher.addURI(CONTENT_STRING, "rules/#", MATCH_ITEM);
                     uriMatcher.addURI(CONTENT_STRING, "read/#", MATCH_READED);
      
              }

	@Override
	public int delete(Uri uri, String selection, String[] selectargs) {
		// TODO Auto-generated method stub
		System.out.println(uriMatcher.match(uri));
		db=dbconn.getWritableDatabase();
		int inflact=0;
		switch(uriMatcher.match(uri)){
		case MATCH_ITEMS:inflact=db.delete(TABLE_NAME,selection,selectargs);break;
		case MATCH_ITEM:	String id = uri.getPathSegments().get(1);
        inflact = db.delete(TABLE_NAME, ID+"="+id, selectargs);
        	break;
		case MATCH_BACKLISTS:inflact=db.delete(TABLE_BACK_LISTS,selection,selectargs);break;
		case MATCH_BACKLIST: String tid = uri.getPathSegments().get(1);
		inflact=db.delete(TABLE_BACK_LISTS, ID+"="+tid, selectargs);break;
		default:   throw new IllegalArgumentException("Unknown URI"+uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return inflact;
	}

	@Override
	public String getType(Uri arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri arg0, ContentValues values) {
		db=dbconn.getWritableDatabase();
		db.insert(TABLE_NAME, null, values);
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		dbconn=new MyDbConnection(getContext());
		db = dbconn.getWritableDatabase();
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectargs,
			String sortorder) {
		// TODO Auto-generated method stub
		db=dbconn.getReadableDatabase();
		SQLiteQueryBuilder qb=new SQLiteQueryBuilder();
		qb.setTables(TABLE_NAME);
		Cursor result;
		switch(uriMatcher.match(uri)){
		case MATCH_ITEMS:result=qb.query(db, projection, null, null, null, null,null);break;
		case MATCH_ITEM:	String id = uri.getPathSegments().get(1);
		result=qb.query(db, projection,ID+"="+id, selectargs, null, null,null);
        	break;
		default:   throw new IllegalArgumentException("Unknown URI"+uri);
		}
		return result;
	}

	@Override
	public int update(Uri uri, ContentValues arg1, String arg2, String[] arg3) {
		// TODO Auto-generated method stub
		db=dbconn.getWritableDatabase();
		int inflact=0;
		switch(uriMatcher.match(uri)){
		case MATCH_ITEMS:inflact=db.update(TABLE_NAME, arg1, arg2, arg3);break;
		case MATCH_ITEM:	String id = uri.getPathSegments().get(1);
        inflact =db.update(TABLE_NAME, arg1, ID+"="+id, arg3);
        	break;
		default:   throw new IllegalArgumentException("Unknown URI"+uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return inflact;
	}

}
